var gulp = require('gulp');

gulp.task('default', function() {
    // place code for your default task here
    console.log('default task...');
});

var uglify = require('gulp-uglify');
var less = require('gulp-less');
var del = require('del');
var minifycss = require('gulp-minify-css');
var clean = require('gulp-clean');
var header = require('gulp-header');

var pkg = require('./package.json');

// banner defined
var banner = [
    '/**',
    ' * <%= project %>',
    ' * @version <%= nowStr %>',
    ' */',
    ''
].join('\n');

var date = new Date(); 
var nowStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
nowStr += ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();

var paths = {
    js:     ['src/js/*.js'],
    less:   ['src/less/*.less'],
    lessDest: ['src/less/main.less']
};


var dir = '';

// 
gulp.task('clean', function () {
    return gulp.src(['build/js', 'build/css'], {read: false, force: true})
        .pipe(clean());
});

gulp.task('js', function () {
    return gulp.src(['src/js/*.js'])
        .pipe(gulp.dest('build/js'));
});

// uglify
gulp.task('uglify', ['js'], function() {
    return gulp.src(['build/js/*.js'])
        // .pipe(rename({suffix: '.min'}))
        .pipe(uglify({
            mangle: {
                except: ['require', 'define', 'export']
            },
            compress: true
        }))
        .pipe(header(banner, { project: project, nowStr: nowStr } ))
        .pipe(gulp.dest('build/js'));
});

// less handle
gulp.task('less', function () {
    gulp.src(paths.less)
        .pipe(less())
        .pipe(gulp.dest('build/css'));
});


// {
//     compatibility: 'ie7',
//     noAdvanced: true
// }

// cssmin 
gulp.task('cssmin', function () {
    gulp.src('build/css/*.css')
        // .pipe(rename({suffix: '.min'}))
        .pipe(minifycss())
        .pipe(header(banner, { project: project, nowStr: nowStr } ))
        .pipe(gulp.dest('build/css'));
});

gulp.task('watch', function() {
    gulp.watch(paths.js, ['js']);
    gulp.watch(paths.less, ['less']);
});


gulp.task('dev', [
    'js', 
    'less'
]);

gulp.task('build', [
    'uglify', 
    'cssmin'
]);

